from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "file:///D:/gitee%E8%BF%9C%E7%A8%8B%E5%85%B3%E8%81%94%E5%BA%93/front-end/Blog_system/BlogPage/html/login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    # 成功登录测试用例
    def LoginSucTest(self):
    # 账号密码输入时都先清空一下
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(2) > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(3) > input").clear()
        # 正确输入账号密码
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(2) > input").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(3) > input").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(4) > button").click()
        #能够找到博客首页，用户的昵称，说明登录成功，反之失败
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.container-left > div > h3")

        self.driver.back()

    # 异常登录测试用例
    def LoginFailTest(self):
    # 正常登录测试完成后，需要重新输入账号密码，需要先清空输入 clear
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(2) > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(3) > input").clear()
        # 输入正确账号 错误密码
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(2) > input").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(3) > input").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.login_container > div > div:nth-child(4) > button").click()
        # 检查是否登录失败
        actual=self.driver.find_element(By.CSS_SELECTOR,"body").text
        #添加屏幕截图，更好观察测试结果
        BlogDriver.getScreeShot()
    # 断言检测是否符合预期
        assert actual == "用户名或密码错误！"


Login = BlogLogin()
Login.LoginSucTest()
Login.LoginFailTest()

















